
<% include("../common/head.html"){} %>

<table title="菜单列表" id="dataGrid" class="easyui-treegrid" 
	data-options="url:'/menu/allMenu',method:'get',rownumbers: true,toolbar:'#menu_search',singleSelect:true,showFooter:true,idField:'id',treeField:'text' ">
<thead>
	<tr>
		<th data-options="field:'text'" width="200">菜单</th>
		<th data-options="field:'url'" width="200">连接地址</th>
		<th data-options="field:'order'" width="100">菜单排序</th>
	</tr>
</thead>
</table>

<div id ="menu_search" class="tableSearch">
	<% if(so.hasRole("admin")){%>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addMenu()">新增菜单</a> 
	<%}%>
	<% if(so.hasRole("admin")){%>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="editMenu()">修改菜单</a> 
	<%}%>
	<% if(so.hasRole("admin")){%>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeMenu()">删除菜单</a>
	<%}%> 
	<a href="#" class="easyui-linkbutton" onclick="managePermission()">权限管理</a>
</div>


<div id="dlgMenu" class="easyui-dialog" style="width:400px;height:200px;padding:10px 20px"
	closed="true" buttons="#dlg-buttons">
	<form id="fm" method="post" novalidate>
        	<div class="fitem">
                <label>上级菜单:</label>
                <input id="parent_id" name="parent_id" class="easyui-combotree" data-options="url:'/menu/allMenu',method:'get'" >
            </div>
            <div class="fitem">
                <label>菜单名称:</label>
                <input name="text" class="easyui-textbox" required="true" >
            </div>
            <div class="fitem">
                <label>连接地址:</label>
                <input name="url" class="easyui-textbox" required="true" >
            </div>
            <div class="fitem">
                <label>菜单排序:</label>
                <input name="order" class="easyui-textbox" required="true" >
            </div>
 	</form>
</div>

<div id="dlg-buttons">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveMenu()" style="width:90px">保存</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlgMenu').dialog('close')" style="width:90px">取消</a>
</div>

<div id="dlgPermission-buttons" class="dialog-button" style="position: relative; top: -1px; width: 377.4px;">
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="savePermission()" style="width:90px">保存</a>
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlgPermission').dialog('close')" style="width:90px">取消</a>
</div> 
    
<div id="dlgPermission" class="easyui-dialog" style="width:400px;height:160px;padding:10px 20px" 
	closed="true" buttons="#dlgPermission-buttons">
        <form id="fmPermission" method="post">
        	<div class="fitem">
                <label>菜单名称:</label>
                <input name="text" class="easyui-textbox" style="width:200px;" required="true" >
            </div>
            <div class="fitem">
                <label>授权权限:</label>
                <select id="permission_id" class="easyui-combotree" name="permission_id" multiple style="width:200px;"
					data-options="valueField:'id',textField:'description',url:'/permission/allPermission'" >
            </div>
        </form>
</div>

<script type="text/javascript">
        var url;
        function addMenu(){
            $('#dlgMenu').dialog('open').dialog('center').dialog('setTitle','新增菜单');
            $('#fm').form('clear');
            url = '/menu/save';
        }
        function editMenu(){
            var row = $('#dataGrid').datagrid('getSelected');
            if (row){
                $('#dlgMenu').dialog('open').dialog('center').dialog('setTitle','修改菜单');
                $('#fm').form('load',row);
                url = '/menu/edit?id='+row.id;
            }
        }
        function saveMenu(){
            $('#fm').form('submit',{
                url: url,
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    var result = eval('('+result+')');
                    if (result.success){
                        $('#dlgMenu').dialog('close');        // close the dialog
                        $('#dataGrid').treegrid('load', {});
                    } else {
                     	$.messager.show({
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    }
                }
            });
        }
        function removeMenu(){
            var row = $('#dataGrid').datagrid('getSelected');
            if (row){
                $.messager.confirm('Confirm','确认删除菜单 '+row.text,function(r){
                    if (r){
                        $.post('/menu/remove',{id:row.id},function(result){
                            if (result.success){
                                $('#dataGrid').treegrid('load', {});
                            } else {
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: result.errorMsg
                                });
                            }
                        },'json');
                    }
                });
            }
        }
        
        function managePermission(){
        	var row = $('#dataGrid').datagrid('getSelected');
            if (row){
            	$('#dlgPermission').dialog('open').dialog('center').dialog('setTitle','权限管理');
            	$('#fmPermission').form('load',row);
            	url = '/menu/managePermission?id='+row.id;
            	//获取已经授权的，将其勾选
            	$.post('/menu/getPermissions',{id:row.id},function(data){
            		var arrayObj = new Array();
            		$.each(data, function(i, r) {
            			arrayObj.push(r.id);
            		});
            		$('#permission_id').combotree('setValues',arrayObj);
            	});
            }
        }
        function savePermission(){
            $('#fmPermission').form('submit',{
                url: url,
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    var result = eval('('+result+')');
                    if (result.success){
                        $('#dlgPermission').dialog('close');        // close the dialog
                    } else {
                     	$.messager.show({
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    }
                }
            });
        }
        
    </script>

<% include("../common/footer.html"){} %>